package com.example.wishstar.view;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.airbnb.lottie.LottieAnimationView;
import com.example.wishstar.R;
import com.example.wishstar.model.Star;
import com.example.wishstar.view.fragment.StarFragment;

public class StarDialogFragment extends DialogFragment implements LoveView.OnCallBack{

    private OnCallBack onCallBack;

    private ImageView imageView = null;
    private LottieAnimationView lottieAnimationView = null;
    private LinearLayout imageLinearLayout, lottieLinearLayout;
    private LoveView loveView = null;
    private int is_like = 0;
    private ImageView imageClose;
    private TextView textViewWishDetails, textViewFinishTime, textViewWishTitle;
    private  Bundle bundle = null;
    private Star star = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialogfragment_star, container, false);

        imageView = view.findViewById(R.id.star_dialog_image_like);
        lottieAnimationView = view.findViewById(R.id.lottie_like);
        imageLinearLayout = view.findViewById(R.id.star_dialog_linearlayout_image_like);
        lottieLinearLayout = view.findViewById(R.id.star_dialog_linearlayout_lottie_like);
        loveView = view.findViewById(R.id.star_dialog_loveview);
        imageClose = view.findViewById(R.id.star_dialog_image_close);
        textViewWishDetails = view.findViewById(R.id.star_dialog_fragment_wishdetails);
        textViewFinishTime = view.findViewById(R.id.star_dialog_fragment_finishtime);
        textViewWishTitle = view.findViewById(R.id.star_dialog_fragment_wishtitle);

        bundle = getArguments();

        loveView.setCallBack(this);

        if (bundle != null) {
            star = new Star();
            star.wishId = bundle.getInt("wishId");
            star.wishTitle = bundle.getString("wishTitle");
            star.wishDetails = bundle.getString("wishDetails");
            star.finishTime = bundle.getString("finishTime");
            if (star.wishId == 0) {
                textViewWishTitle.setVisibility(View.GONE);
                textViewWishDetails.setText(getContext().getString(R.string.star_dialogfragment_no_many_star));
                textViewFinishTime.setText(getContext().getString(R.string.star_dialogfragment_finishtime_text) + getContext().getString(R.string.star_dialogfragment_xxx_time_text));
            }
            else {
                textViewWishTitle.setText("《" + star.wishTitle + "》");
                textViewWishDetails.setText(star.wishDetails);
                textViewFinishTime.setText(getContext().getString(R.string.star_dialogfragment_finishtime_text) + star.finishTime);
            }
        }
        lottieAnimationView.setVisibility(View.GONE);
        lottieLinearLayout.setVisibility(View.GONE);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                is_like = 1;
                imageView.setVisibility(View.GONE);
                imageLinearLayout.setVisibility(View.GONE);
                lottieLinearLayout.setVisibility(View.VISIBLE);
                lottieAnimationView.setVisibility(View.VISIBLE);
                lottieAnimationView.playAnimation();
            }
        });

        lottieAnimationView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                is_like = 0;
                lottieAnimationView.setVisibility(View.GONE);
                lottieLinearLayout.setVisibility(View.GONE);
                imageLinearLayout.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
            }
        });

        imageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        return view;
    }

    @Override
    public void callback() {
        if(is_like == 0) {
            is_like = 1;
            imageView.setVisibility(View.GONE);
            imageLinearLayout.setVisibility(View.GONE);
            lottieLinearLayout.setVisibility(View.VISIBLE);
            lottieAnimationView.setVisibility(View.VISIBLE);
            lottieAnimationView.playAnimation();
        }
    }

    public void dismiss() {
        super.dismiss();
    }

    public void onDismiss(@NonNull DialogInterface dialog) {
        onCallBack.callback(star.wishId, is_like);
        super.onDismiss(dialog);
    }

    public void setCallBack(OnCallBack onCallBack){
        this.onCallBack = onCallBack;
    }

    public interface OnCallBack{
        void callback(int wishid, int is_like);
    }
}
